/*
 * Copyright (c) 2018-2023 北京华恒盛世科技有限公司
 * QUICKPOOL is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
*/

#include "subcmd.h"

static void postmsg_usage(const char *);

/**
 * @brief       qp job post handler, to post message to a job in the cluster.
 *
 * @param[in]   argc          #1: number of input argument
 * @param[in]   argv          #2: string array of the input argument
 *
 * @retval      0             #1: succeed
 * @retval      1             #2: input option is wrong or kind of input argument not specified
 * @retval      2             #3: argument has syntax error
 * @retval      3             #4: failed to post message to the job
 */
int
cmd_post_message(int argc, char **argv)
{
    char *strMsg=NULL;
    while (TRUE) {
        int option_index = 0;
        static struct option long_options[] = {
            {"help", no_argument, 0, 'h'},
            {"message", required_argument, 0, 'd'},
            {NULL, 0, 0, 0}
        };

        int opt;
        opt = getopt_long(argc, argv, "hd:", long_options, &option_index);
        if (-1 == opt) {
            break;
        }

        switch (opt) {
        case 'd':
            strMsg = optarg;
            break;
        case 'h':
            postmsg_usage(argv[0]);
            exit(0);
        case '?':
        default:
            postmsg_usage(argv[0]);
            exit(1);
        }
    }

    if (NULL == strMsg) {
        fprintf(stderr, "Must specify a message to be posted.\n");
        postmsg_usage(argv[0]);
        exit(1);
    }

    if (strlen(strMsg) > MAXMESSAGELENGTH) {
        fprintf(stderr, "Message is longer than %d.\n", MAXMESSAGELENGTH);
        exit(2);
    }

    if (argc <= optind) {
        fprintf(stderr, "Must specify a job ID.\n");
        postmsg_usage(argv[0]);
        exit(1);
    }

    // get the job ID for posting message to it.
    JOBID_T jobId;
    if (get_one_jobid(argv[argc - 1], &jobId)) {
        exit(2);
    }

    struct request messageRequest;
    messageRequest.number = 2;
    messageRequest.keyvalues = (struct keyvalue *)calloc(messageRequest.number,
                                                         sizeof(struct keyvalue));
    if (NULL == messageRequest.keyvalues) {
        fprintf(stderr, "calloc(%ld) failed due to %m.\n",
                messageRequest.number*sizeof(struct keyvalue));
        exit(4);
    }

    messageRequest.keyvalues[0].key = REQUEST_KEY_JOBIDS;
    messageRequest.keyvalues[0].value = (char *)calloc(32, sizeof(char));
    if (NULL == messageRequest.keyvalues[0].value) {
        fprintf(stderr, "calloc(%d) failed due to %m.\n", 32);
        exit(4);
    }
    snprintf(messageRequest.keyvalues[0].value, 32, "%lld", jobId);

    messageRequest.keyvalues[1].key = REQUEST_KEY_JOB_MESSAGE;
    messageRequest.keyvalues[1].value = copy_string(strMsg);

    int ret;
    ret = client_post_jobmessage(&messageRequest, NULL);
    if (0 > ret) {

        gf_xdr_free(gf_xdr_request, &messageRequest);
        exit(3);
    }

    gf_xdr_free(gf_xdr_request, &messageRequest);

    char strJobId[32];
    fprintf(stdout, "Posted message to job <%s>.\n", jobid_2string(jobId, strJobId));

    exit(0);
} // end function cmd_post_message

static void
postmsg_usage(const char *cmd)
{
    fprintf(stdout, "qp job %s usage: [-h, --help] -d, --message \"message...\" jobId\n", cmd);

    return;
}
